package com.leaf.u_spring.chapter13;


import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * 配置将缓存条目存储在Redis服务器的缓存管理器
 *
 */
@Configuration
@EnableCaching		
public class RedisCachingConfig {

	/**
	 * 声明Redis缓存管理器  
	 * @param cm
	 * @return
	 */
	@Bean
	public CacheManager cacheManager(RedisTemplate<String, Object> redisTemplate) {
		return new RedisCacheManager(redisTemplate);
	}
	
	/**
	 * Redis连接工厂bean
	 * @return
	 */
	@Bean
	public JedisConnectionFactory redConnectionFactory() {
		JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
		//默认localhost 6379
		jedisConnectionFactory.afterPropertiesSet();
		return jedisConnectionFactory;
	}
	
	/**
	 * RedisTemplate bean
	 * @param rediscf
	 * @return
	 */
	@Bean
	public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory rediscf){
		RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
		redisTemplate.setConnectionFactory(rediscf);
		redisTemplate.afterPropertiesSet();
		return redisTemplate;
	}
  
}